using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using eee.Sheffield.PZ.Imaging;
using System.IO;

namespace eee.Sheffield.PZ.QAAS
{
    public partial class ZouRockettTrackingFilterControl : FilterControl
    {
        #region Fields
        private PZPoint _startPoint;
        private PZDirection _startDirection;
        private int _scanlineWidth;
        private double _background;
        private List<TrackResult> _trackResultList = null;
        private PostProcessingTrackingResults _postProcess;
        #endregion

        #region Constructor
        public ZouRockettTrackingFilterControl()
        {
            InitializeComponent();
            _flag = "Zou Rockett Tracking Filter";            
        }

        public ZouRockettTrackingFilterControl(MainForm mainForm, ImageDoc imageDoc)
            : base(mainForm, imageDoc)
        {
            InitializeComponent();
            _flag = "Zou Rockett Tracking Filter";
        }
        #endregion

        #region button Events
        private void buttonApply_Click(object sender, EventArgs e)
        {
            double startX = Convert.ToDouble(textBoxStartX.Text);
            double startY = Convert.ToDouble(textBoxStartY.Text);
            double startDX = Convert.ToDouble(textBoxStartDX.Text);
            double startDY = Convert.ToDouble(textBoxStartDY.Text);
            _startPoint = new PZPoint(startX, startY);
            _startDirection = new PZDirection(startDX, startDY);
            _scanlineWidth = Convert.ToInt16(textBoxScanlineWidth.Text);
            _background = Convert.ToDouble(textBoxBackground.Text);

            TaskTryConsequtiveScanlineTracking tryTrack = new TaskTryConsequtiveScanlineTracking(_startPoint, _startDirection,
                _scanlineWidth, _background);

            

            Bitmap srcImage = (Bitmap)_imageDoc.Image.Clone();
            eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo8bbpIndexed(ref srcImage);
            Bitmap dstImage = tryTrack.Apply(srcImage);

            _trackResultList = tryTrack.TrackResultList;

            ShowImage(dstImage, "Zou&Rockett Track Result");
        }

        /// <summary>
        /// post process
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonPostProcess_Click(object sender, EventArgs e)
        {
            if (_trackResultList == null)
            // load track results
            {
                string fileName;
                string pathName;
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    fileName = openFileDialog.FileName;
                    pathName = Path.GetDirectoryName(fileName);
                    _postProcess = new PostProcessingTrackingResults();
                    _postProcess.PathName = pathName;
                    _postProcess.ReadFile();
                    _postProcess.SrcImage = (Bitmap)_imageDoc.Image.Clone();
                }
            }
            else
            {
                _postProcess = new PostProcessingTrackingResults(_trackResultList, (Bitmap)_imageDoc.Image.Clone());
            }

            // debug
            //List<TrackResult> tempTR = _postProcess.FindCPInTrackResults(new PZPoint(158, 137));
            //Bitmap tempImage = _postProcess.DrawTrackResultsCentreLines(tempTR, _postProcess.SrcImage);
            //tempImage.Save("temp.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

            // remove duplicate
            System.Console.WriteLine("total length: " + _postProcess.TotalLength());
            _postProcess.RemoveDuplicates();
            System.Console.WriteLine("total length: " + _postProcess.TotalLength());

            // remove short results
            _postProcess.LengthThreshold = 3;
            _postProcess.RemoveShortResults();
            System.Console.WriteLine("total length: " + _postProcess.TotalLength());

            // remove disconnected centre points
            _postProcess.RemoveDisconnectedCPs(3);
            System.Console.WriteLine("total length: " + _postProcess.TotalLength());

            // show image
            Bitmap dstImage = _postProcess.DrawTrackResultsCentreLines();
            ShowImage(dstImage, "Post Process Result");
            buttonSaveDiagram.Enabled = true;
        }

        /// <summary>
        /// save post process result, network diagram
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSaveDiagram_Click(object sender, EventArgs e)
        {
            string fileName = "";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                fileName = saveFileDialog.FileName;
                if (fileName != "")
                    _postProcess.WriteDiagram(fileName);
            }
        } 
        #endregion


    }
}
